package com.wondertek.poms.sync.manager.main;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.SyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p><b>SubSetManager Description:</b> 子集流程</p>
 * @author douzi
 * <b>DATE</b> 2019年9月6日 下午4:06:53
 */
@Slf4j
@Component
public class NoProductSubSetManager extends ParentManager {
    @Autowired
    NoProductMainAssertManager mainAssertManager;
    @Autowired
    IContentService contentService;

    /**
     * <p><b>Title:</b> liveBillProcess</p>
     * <p><b>Description:</b> 子集流程</p>
     *
     * @param vo 内容对象
     * @author douzi
     */
    public void subSetProcess(Content c, SyncTask st, List<PrdInfo> normalList) {
        switch (c.getStatus().intValue()) {
            case Constants.ASSET_STATUS_0:
            case Constants.ASSET_STATUS_1:
            case Constants.ASSET_STATUS_3:
                subSetUpdateProcess(c, st, normalList);
                break;
            case Constants.ASSET_STATUS_2:
            	mainDelProcess(st, "normal-subset");
                break;
            default:
            	log.info("普通包主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() +  "|不属于任何一种媒资状态:" + c.getStatus());
                break;
        }
    }

	/**
     * <p><b>Title:</b> subSetProcess</p>
     * <p><b>Description:</b> 子集媒资流程 </p>
     *
     * @param vo 内容对象
     * @author douzi
     */
    private void subSetUpdateProcess(Content c, SyncTask st, List<PrdInfo> normalList) {
    	normalList.stream().parallel().forEach(pi -> {
    		List<PrdCont> dbPPcs = prdContService.findPcByContentIdInPrdInfo(c.getSerialContentId(), pi.getPrdInfoId());
    		if (dbPPcs == null || dbPPcs.size() <= 0) {
    			//TODO 需要下线现有子集
    			log.info("普通包子集主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目不存在:" + c.getLiveContentId() + "|商品包：" + pi.getPrdInfoId());
    		} else if (dbPPcs.size() == 1) {
        		pcProcess(c, pi);
        	} else {
        		//TODO 去重壳后在入包 暂时不做
        		log.error("普通包子集主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目存在多个:" + c.getLiveContentId() + "|个数：" + dbPPcs.size());
        	}
    	});
    }
    
    /**
	 * <p><b>Title:</b> pcProcess</p>
	 * <p><b>Description:</b> 子集根据数据库是否存在走后续流程</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @param dbPPcs
	 */
	private void pcProcess(Content c, PrdInfo prdInfo) {
		if (prdInfo != null && prdInfo.getIsPublish() == 2 && 
				(prdInfo.getPrdInfoType() == null || prdInfo.getPrdInfoType() != 3)) {
			List<PrdCont> prdContDbs = prdContService.getPrdContByMmsIdInPrdInfo(c.getAssetId(), prdInfo.getPrdInfoId());
			if (prdContDbs.size() <= 0) {
	            inPrdInfo(prdInfo, c, null);
	        } else if (prdContDbs.size() == 1) {
	        	PrdCont pcDb = prdContDbs.get(0);
	        	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
	        	if (pcDb.getContentId().equals(c.getContentId())) {
	                inPrdInfo(prdInfo, c, pcDb);
	        	} else {
	        		findOptimalPrdCont(c, prdInfo, pcDb, "主流程-子集");
	        	}
	        } else {
				//TODO 去重子集后在入包 暂时不做
				log.error("子集主流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目存在多个:" + c.getLiveContentId() + "|个数：" + prdContDbs.size());
			}
		}

	}
}
